﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SynoManager.DownloadStation
{
    public partial class DownloadTaskPropertiesForm : Form
    {
        private readonly DownloadTask task;
        private readonly DownloadTaskFileCollection files;

        public DownloadTaskPropertiesForm()
        {
            InitializeComponent();
        }

        public DownloadTaskPropertiesForm(DownloadTask task)
            :this()
        {
            this.task = task;

            taskLabel.Text = task.Filename;

            // General Tab
            filenameTextBox.Text = task.Filename;
            targetFolderLabel.Text = task.DownloadLocation;
            fileSizeLabel.Text = task.SizeTotal.ToString();
            dateCreatedLabel.Text = task.TimeCreated.ToString();
            urlTextBox.Text = task.Url;
            usernameLabel.Text = task.Username;

            // Transfer Tab
            stateLabel.Text = task.State.ToString();
            sizeLabel.Text = task.SizeCurrent.ToString() + ((task.UploadTotal > 0) ? task.UploadTotal.ToByteString() : "");
            progressLabel.Text = (int)(task.Progress * 100) + "%";
            progressBar.Value = (int)(task.Progress * 100);
            progressBar.State = DownloadGuiTab.GetProgressState(task);
            speedLabel.Text = String.Format("Down: {0}/s, Up: {1}/s", task.RateCurrent, task.RateUpload);
            peersLabel.Text = task.PeersTotal.ToString();
            connectedPeersLabel.Text = task.PeersConnected.ToString();
            startTimeLabel.Text = task.TimeStarted.ToString();
            timeRemainingLabel.Text = task.RemainingEstimate.ToString();
            totalPiecesLabel.Text = task.PiecesTotal.ToString();
            downloadedPiecesLabel.Text = task.PiecesDownloaded + "/" + task.PiecesAvailable;

            if (task.TaskType == DownloadTaskType.BitTorrent)
            {
                // Files Tab
                files = task.Files;
                foreach (DownloadTaskFile file in files)
                {
                    filesListBox.Items.Add(file, file.Download);
                }
            }
            else
            {
                tabControl.TabPages.Remove(filesTab);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            for(int i=0;i<files.Count;i++)
            {
                DownloadTaskFile file = files[i];
                file.Download = filesListBox.GetItemChecked(i);
            }
            files.Save();
            DialogResult = DialogResult.OK;
        }
    }
}
